import { getJson, saveJson } from '../../utils.js'

import { fileURLToPath } from 'node:url'
const getFilePath = path => fileURLToPath(new URL(path, import.meta.url))

const src = getFilePath('src/淮阳区机关事业单位.json')
const dist = getFilePath('dist/淮阳区机关事业单位.json')
const data = getJson(src)

function getData(data) {
  const newData = []

  if (Array.isArray(data)) {
    data.map(item => {
      newData.push({
        text: item,
        value: item,
      })
    })
  } else if (typeof data == 'object') {
    for (const key in data) {
      const name = key
      const value = data[key]
      let children = getData(value)

      const newItem = {
        text: name,
        value: name,
      }

      if (children && children.length) newItem.children = children

      newData.push(newItem)
    }
  }

  return newData
}

const newData = getData(data)

saveJson(dist, newData)

// saveFile(getFilePath('1.json'), JSON.stringify(newData, null, '\t'))
